/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.camelot.ccr.global.controller.BaseController;
import com.camelot.ccr.global.util.Pager;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.dao.SponsorDao;
import com.camelot.ccr.system.persistence.Driver;
import com.camelot.ccr.system.persistence.Sponsor;
import com.camelot.ccr.system.persistence.Team;

/**
 * <pre>
 * 赞助商Controller
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-23 下午2:11:38
 */
@Controller
@RequestMapping("/manage/sponsor")
public class SponsorController extends BaseController {

	@Resource
	private SponsorDao sponsorDao;
	
	/**
	 * 赞助商图片地址
	 */
	public static final String headUrlPath="/upload/images/sponsor/";
	
	/**
	 * <pre>
	 * 查询车队信息
	 * </pre>
	 * @param sponsor
	 * @param model
	 * @return 车队列表
	 * @author Liu Jun, 2014-5-23 下午5:36:47
	 */
	@RequestMapping("/list")
	public String list(Sponsor sponsor,ModelMap model,HttpServletRequest request){
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		Pager<Sponsor> pager = sponsorDao.findPageList(Integer.parseInt(curpage),100 , sponsor);
		
		model.put("pager", pager);
		return "/manage/sponsor/sponsor_index";
	}
	
	/**
	 * <pre>
	 * 查询车队信息
	 * </pre>
	 * @param sponsor
	 * @param model
	 * @return 车队列表
	 * @author Liu Jun, 2014-5-23 下午5:36:47
	 */
	@RequestMapping("/add")
	public String add(Sponsor sponsor,ModelMap model,HttpServletRequest request){
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		Pager<Sponsor> pager = sponsorDao.findPageList(Integer.parseInt(curpage),100 , sponsor);
		
		model.put("pager", pager);
		return "/manage/sponsor/sponsor_add";
	}
	
	@RequestMapping("/save")
	public String save(@RequestParam("logoUrl")CommonsMultipartFile file,HttpServletRequest request) throws Exception{
		String sponsorName = request.getParameter("sponsorName");
		String sponsorUrl = request.getParameter("sponsorUrl");
		
		String path = request.getRealPath(headUrlPath);
		
		Sponsor sponsor = new Sponsor();
		sponsor.setSponsorName(sponsorName);
		sponsor.setSponsorUrl(sponsorUrl);
		sponsor.setLogoUrl(path);
		
		sponsorDao.saveSponsor(sponsor, file);
		return "redirect:list";
	}
	
}
